package org.gongliang.jsmile.authc.sys.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.gongliang.jsmile.authc.sys.entity.SysDepartment;
import org.gongliang.jsmile.authc.sys.service.SysDepartmentService;
import org.gongliang.jsmile.authc.sys.vo.SysDepartmentVo;
import org.gongliang.jsmile.common.base.BaseController;
import org.gongliang.jsmile.common.base.JSmileResult;
import org.gongliang.jsmile.common.enums.KException;
import org.gongliang.jsmile.common.kit.web.ShiroKit;
import org.gongliang.jsmile.common.kit.web.WebKit;
import org.gongliang.jsmile.config.log.info.KLog;
import org.gongliang.jsmile.config.log.info.LogType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
* @desc   控制器
* @author 龚亮
* @version 1.0
* @date 2018年08月25日 04:57:24
*/
@Controller
@RequestMapping("/sys_dept")
@Api(tags = "部门管理")
public class SysDepartmentController extends BaseController {

    @Autowired
    private SysDepartmentService sysDepartmentService;
    
	@GetMapping("/main")
	public String main() {
		return "sys/Department/dept";
	}

	@GetMapping("/add")
	public String add() {
		return "sys/Department/add";
	}
	@GetMapping("/edit")
	public String edit() {
		return "sys/Department/edit";
	}


	@ResponseBody
	@RequestMapping("/findAll")
	@ApiOperation(value = "获取部门列表", notes = "不分页获取所有部门")
	@KLog(title = "部门管理->获取部门列表", type = LogType.QUERY)
	public JSmileResult findAll(HttpServletRequest request) {
		Map<String, Object> reqMap = WebKit.getParams(request);
		List<SysDepartmentVo> list = sysDepartmentService.findAllByVo(reqMap);
		JSmileResult result = JSmileResult.success();
		result.setResData(list);
		return result;
	}

	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("sys:dept:add")
	@KLog(title = "部门管理->添加部门信息", type = LogType.INSERT)
	public JSmileResult save(SysDepartment dept) {
		JSmileResult result=JSmileResult.success("保存部门信息成功!");
		dept.setCreateDate(new Date());
		dept.setCreator(ShiroKit.getUserId());
		if(!sysDepartmentService.saveOne(dept)) {
			result.setResState(JSmileResult.STATE_ERROR);
			result.setResMsg("保存部门信息失败!");
		}
		return  result;
	}
	
	@ResponseBody
	@PostMapping("/update")
	@RequiresPermissions("sys:dept:update")
	@KLog(title = "部门管理->修改部门信息", type = LogType.UPDATE)
	public JSmileResult update(SysDepartment dept) {
		JSmileResult result=JSmileResult.success("修改部门信息成功!");
		dept.setUpdater(ShiroKit.getUserId());
		dept.setUpdateDate(new Date());
		if(!sysDepartmentService.updateById(dept)) {
			result.setResState(JSmileResult.STATE_ERROR);
			result.setResMsg("修改部门信息失败!");
		}
		return  result;
	}
	


	/**
	 * 根据一组id删除
	 */
	@KLog(title = "部门管理->删除部门", type = LogType.DELETE)
	@RequiresPermissions("sys:dept:delete")
	@ResponseBody
	@PostMapping("/deleteByIds")
	@ApiOperation(value = "批量删除部门", notes = "根据ids数组批量删除")
	public JSmileResult deleteByIds(@RequestParam(value = "ids[]", required = true) String[] ids) {
		if (ids == null || ids.length == 0) {
			return JSmileResult.error(KException.IDS_NOT_NULL);
		}
		return sysDepartmentService.deleteDepts(ids) ? JSmileResult.success("删除部门成功!") : JSmileResult.error("删除部门失败!");
	}
}
